package com.sgb.kjwl.view.ui.adapter.home;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.lease.LeaseOrderEntity;
import com.sgb.kjwl.model.entity.lease.LeaseOrderInvoiceEntity;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;

import java.util.List;

/**
 * 发票信息列表itme
 */
public class LeaseInvoiceAdapter extends BaseRecycleAdapter<LeaseOrderInvoiceEntity> {

    public LeaseInvoiceAdapter(Context context, List<LeaseOrderInvoiceEntity> datas) {
        super(context, R.layout.item_lease_invoice, datas);
    }

    @Override
    public void convert(ItemViewHolder holder, final LeaseOrderInvoiceEntity entity) {
        holder.setBinding(BR.viewModel, entity);
        RelativeLayout llItem = holder.itemView.findViewById(R.id.rl_item);
        ImageView ivSelected = holder.itemView.findViewById(R.id.iv_selected);
        if (entity.isCheck()) {
            llItem.setBackgroundResource(R.drawable.bg_blue_solid_stroke_r4);
            ivSelected.setVisibility(View.VISIBLE);
        } else {
            llItem.setBackgroundResource(R.drawable.bg_garyf5_solid_stroke_r4);
            ivSelected.setVisibility(View.GONE);
        }

        llItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clearChecked();
                entity.setCheck(true);
                notifyDataSetChanged();
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(entity);
                }
            }
        });
    }

    private void clearChecked() {
        for (int i = 0; i < datas.size(); i++) {
            datas.get(i).setCheck(false);
        }
    }

    public interface OnItemClickListener {
        void onItemClick(LeaseOrderInvoiceEntity entity);
    }

    private OnItemClickListener onItemClickListener;

    public void setOnOnItemClickListener(OnItemClickListener listener) {
        this.onItemClickListener = listener;
    }

}
